﻿Imports Mystery

Public Class ContentButtonAttribute
    Inherits MysteryDefaultClassAttribute

    Public Property implementing_type As Type
    Public Property normal_icon As String

    Private generic As Type = GetType(ContentButton(Of ))

    Private _button_activator As IActivator

    Public Overrides Sub setUp()
        If implementing_type IsNot Nothing Then
            implementing_type.GetInterfaceMap(GetType(IButton))
        Else
            implementing_type = generic.MakeGenericType(used_in)
        End If

        _button_activator = getGlobalObject(Of FastActivator).getActivator(implementing_type)

    End Sub

    Public Function createButton(content As IContent) As IContentButton
        Dim b As IContentButton = _button_activator.createInstance()
        b.content = content
        Return b
    End Function

End Class

Public Class ContentButton(Of T As IContent)
    Implements IContentButton


    Public Event Clicked() Implements IButton.Clicked

    Private _image_uri As Uri
    Dim button_text As ReferenceText

    Public Sub New()
        Dim ct As ContentType = getMystery.AssemblyRegister.getMysteryClassAttribute(Of ContentType, T)()
        Dim cb As ContentButtonAttribute = getMystery.AssemblyRegister.getMysteryClassAttribute(Of ContentButtonAttribute, T)()
        button_text = GetType(T).getFirstAttribute(Of ReferenceText)()

        If String.IsNullOrEmpty(cb.normal_icon) Then
            _image_uri = New Uri("/MysteryControls;component/Images/object.png", UriKind.Relative)
        Else
            _image_uri = New Uri(cb.normal_icon, UriKind.RelativeOrAbsolute)
        End If
    End Sub

    Public Function getImage() As System.Windows.Controls.Image Implements IButton.getImage
        Dim result As New Image()
        Try
            result.Source = New BitmapImage(_image_uri)
        Catch ex As Exception

        End Try
        Return result
    End Function

    Public Function getText() As String Implements IButton.getText
        If button_text IsNot Nothing Then Return button_text.getReferenceText(content)
        If content IsNot Nothing Then Return content.guid.ToString
        Return "see"
    End Function

    Public Sub Press() Implements IButton.Press
        Dim ir As InterfaceStatus = getGlobalObject(Of InterfaceStatus)()

        ir.showContent(content, False)

        RaiseEvent Clicked()
    End Sub

    Private _content_guid As Guid

    Public Property content As Mystery.IContent Implements IContentButton.content
        Get
            If Guid.Empty = _content_guid Then Return Nothing
            Return getGlobalObject(Of IContentDispatcher).getContent(Of IContent)(_content_guid)
        End Get
        Set(value As Mystery.IContent)
            _content_guid = If(value Is Nothing, Guid.Empty, value.guid)
        End Set
    End Property
       
End Class